package com.company.cloud.club.modules.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.cloud.club.modules.core.dto.Result;
import com.company.cloud.club.modules.core.util.DateUtil;
import com.company.cloud.club.modules.core.util.ValidateUtil;
import com.company.cloud.club.modules.db.domain.StaffLog;
import com.company.cloud.club.modules.db.domain.UserInfo;
import com.company.cloud.club.modules.dto.LayTableResult;
import com.company.cloud.club.modules.service.StaffLogService;
import com.company.cloud.club.modules.service.UserInfoService;
import com.company.cloud.common.auth.annotation.StaffLogc;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Fangjun
 */
@Api(tags = "员工记录")
@RequestMapping("/staffLog")
@AllArgsConstructor
@RestController
public class StaffLogController {

    @Autowired
    private StaffLogService staffLogService;

    @Autowired
    private UserInfoService userInfoService;

    //案列列表接口
    @GetMapping(value = "list")
    public LayTableResult<StaffLog> list(@Valid @ModelAttribute StaffLog params) {
        List<StaffLog> list = new ArrayList<>();
        Integer begin = params.getCurrentPage();
        Integer pageSize = params.getPageSize();
        Integer currentPage = (begin - 1) * pageSize;
        if (ValidateUtil.isValid(params.getUserName())) {
            final List<UserInfo> list2 = userInfoService.list(new QueryWrapper<UserInfo>().like("nick_name", params.getUserName()).eq("deleted", 0));
            if (list2.size() > 0) {
                List<Integer> ids = new ArrayList<>();
                list2.forEach(m -> {
                    ids.add(m.getId());
                });
                final List<StaffLog> list1 = staffLogService.list(new QueryWrapper<StaffLog>()
                        .in("user_id", ids)
                        .eq("deleted", 0)
                        .last("limit " + currentPage + "," + pageSize)
                        .orderByDesc("time_created"));
                list1.forEach(m -> {
                    final UserInfo byId = userInfoService.getById(m.getUserId());
                    if (ValidateUtil.isValid(byId)) {
                        m.setUserName(byId.getNickName());
                    }
                    list.add(m);
                });
                int count = staffLogService.count(new QueryWrapper<StaffLog>(params));
                return new LayTableResult<>(Result.SUCCESS_CODE, "获取成功", list1, count);
            }
        }

        final List<StaffLog> list1 = staffLogService.list(new QueryWrapper<StaffLog>()
                .eq("deleted", 0)
                .last("limit " + currentPage + "," + pageSize).orderByDesc("time_created"));
        list1.forEach(m -> {
            final UserInfo byId = userInfoService.getById(m.getUserId());
            if (ValidateUtil.isValid(byId)) {
                m.setUserName(byId.getNickName());
            }
            list.add(m);
        });
        int count = staffLogService.count(new QueryWrapper<StaffLog>(params));
        return new LayTableResult<>(Result.SUCCESS_CODE, "获取成功", list, count);
    }

    @PostMapping(value = "save")
    @StaffLogc(title = "员工添加", action = "员工操作", actionAdo = "员工列表")
    public Result<Boolean> save(@RequestBody @Valid StaffLog params) {
        StaffLog staffLog = new StaffLog(0);
        staffLog.setTitle(params.getTitle());
        staffLog.setAction(params.getAction());
        staffLog.setActionAdo(params.getActionAdo());
        staffLog.setActionUrl(params.getActionUrl());
        staffLog.setActionParam(params.getActionParam());
        staffLog.setActionBody(params.getActionBody());
        staffLog.setActionDate(DateUtil.format(new Date(), DateUtil.DATE_TIME_PATTERN));
        staffLog.setUserId(params.getUserId());
        return Result.ok(staffLogService.saveOrUpdate(staffLog));
    }

    @GetMapping(value = "del")
    @StaffLogc(title = "员工删除", action = "员工删除", actionAdo = "员工列表")
    public Result<Boolean> del(@Valid @ModelAttribute String id) {
        StaffLog staffLog = staffLogService.getById(Integer.parseInt(id));
        if (ValidateUtil.isEmpty(staffLog)) {
            throw new RuntimeException("数据不存在");
        }
        staffLog.setDeleted(1);
        return Result.ok(staffLogService.saveOrUpdate(staffLog));
    }
}
